﻿using DmxTestTool.Model;
using DmxTestTool.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DmxTestTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<DMXViewUIModel> DMXViewUIModels;
        public MainWindow()
        {
            InitializeComponent();
            DMXViewUIModels = new List<DMXViewUIModel>();

            this.Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object sender, EventArgs e)
        {
            foreach (var item in DMXViewUIModels)
            {
                item.Dispose();
            }
        }

        private void Add_Btn_Click(object sender, RoutedEventArgs e)
        {
            AddDMXTestWindows addDMX = new AddDMXTestWindows();
           if(addDMX.ShowDialog()==true)
            {

                var DMXViewUIModel = new DMXViewUIModel(addDMX.DMXTestInfo);
                DMXViewUIModel.DMXViewUIModelEvent += DMXViewUIModel_DMXViewUIModelEvent;
                DMXViewUIModels.Add(DMXViewUIModel);

                var ui = new UI.DMXViewUIControl();
                ui.DMXID = DMXViewUIModel.ID;
                ui.SetBinding(DMXViewUIControl.DmxViewItemSourceProperty, new Binding("ItemSource") { Source = DMXViewUIModel });
                ui.DataContext = DMXViewUIModel;

                TabItem tabItem = GetTabItem(DMXViewUIModel);
                tabItem.Content = ui;

                tab.Items.Add(tabItem);
                tab.SelectedItem = tabItem;
            }
       
        }
        private TabItem GetTabItem(DMXViewUIModel dMXView)
        {
            TabItem tabItem = new TabItem();
            StackPanel sp = new StackPanel() { Orientation= Orientation.Horizontal,Margin=new Thickness(10,2,10,2)};
            sp.Children.Add(new TextBlock { Text=dMXView.DMXName,FontSize=16});
            var btn = new Button { Margin = new Thickness(10, 0, 0, 0),Padding=new Thickness(5,0,5,0),Content="X" };
            btn.SetBinding(Button.CommandProperty, new Binding("CloseBtn") { Source = dMXView, Mode = BindingMode.OneWay });
            btn.SetBinding(Button.CommandParameterProperty, new Binding("ID") { Source = dMXView, Mode = BindingMode.OneWay });

            sp.Children.Add(btn);
            tabItem.Header = sp;
            return tabItem;
        }

        private void DMXViewUIModel_DMXViewUIModelEvent(object sender, string action)
        {
            this.Dispatcher.Invoke(()=> {
                if (action == "CloseTab")
                {
                    DMXViewUIModel m = sender as DMXViewUIModel;
                    var items = tab.Items;
                    foreach (var item in items)
                    {
                        DMXViewUIControl dMXViewUIControl = ((TabItem)item).Content as DMXViewUIControl;
                        if (dMXViewUIControl == null)
                            continue;
                        if (dMXViewUIControl.DMXID == m.ID)
                        {
                            m.Dispose();
                            tab.Items.Remove(item);
                            DMXViewUIModels.Remove(m);
                            break;
                        }
                    }
                }
            });
            
        }
    }
}
